package com.judian.smart.decoration;

import android.graphics.Rect;
import android.view.View;

import androidx.recyclerview.widget.RecyclerView;

public class LinearSpacingItemDecoration extends RecyclerView.ItemDecoration {

    private int spacing;//间隔
    private boolean includeEdge;//是否包含边缘

    private int top, left, bottom, right;
    private boolean isFour = false;

    public LinearSpacingItemDecoration(int spacing, boolean includeEdge) {
        this.spacing = spacing;
        this.includeEdge = includeEdge;
    }

    public LinearSpacingItemDecoration(int top, int left, int bottom, int right) {
        isFour = true;
        this.top = top;
        this.bottom = bottom;
        this.left = left;
        this.right = right;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        int position = parent.getChildAdapterPosition(view);
        if (isFour) {
            if (position == 0) {
                outRect.top = top;
            }
            outRect.bottom = bottom;
            outRect.left = left;
            outRect.right = right;
        } else {
            if (includeEdge) {
                if (position == 0) {
                    outRect.top = spacing;
                }
                outRect.bottom = spacing;
                outRect.left = spacing;
                outRect.right = spacing;
            } else {
                outRect.bottom = spacing;
            }
        }
    }
}
